#define _CRT_SECURE_NO_WARNINGS 1


#include<iostream>

using namespace std;

struct ListNode 
{
	int val;
	ListNode* next;
	ListNode(int x) : val(x), next(NULL) { }
	
};

bool hasCycle(ListNode* head)
{
	ListNode* slow, * fast;
	slow = fast = head;
	while (fast && fast->next)
	{
		slow = slow->next;
		fast = fast->next->next;
		if (slow == fast)
		{
			return true;
		}
	}
	return false;
}

int main()
{
	ListNode* node1 = new ListNode(3);
	ListNode* node2 = new ListNode(2);
	ListNode* node3 = new ListNode(0);
	ListNode* node4 = new ListNode(-4);
	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	node4->next = node2;
	cout << hasCycle(node1) << endl;

	return 0;
}